package de.chessgame.gamelogic.piece;

import java.util.ArrayList;

import de.chessgame.gamelogic.Coordinate;

public class Knight extends Piece {

	public Knight(Fraction fraction) {
		super(Type.KNIGHT, fraction);
	}

	@Override
	public void calculatePossibleMoves(Piece[][] content, int xOld, int yOld, boolean inCheck) {
		possibleMoves.clear();
		super.calculatePossibleMoves(content, xOld, yOld, inCheck);
		possibleMoves.addAll(givePossibleMovesForKnight(content, xOld, yOld));
	}
	
	@Override
	public boolean canMove(Piece[][] content, int xOld, int yOld, boolean inCheck) {
		return givePossibleMovesForKnight(content, xOld, yOld).size() != 0;
	}
	
	
	private ArrayList<Coordinate> givePossibleMovesForKnight(Piece[][] content, int xOld, int yOld) {
		ArrayList<Coordinate> list = new ArrayList<Coordinate>();
		for (int xMove = 1, yMove = 2, i = 0; i < 2; xMove = 2, yMove = 1, i++) {
			for (int xTry = xOld - xMove, j = 0; j < 2; xTry = xOld + xMove, j++) {
				for (int yTry = yOld - yMove, k = 0; k < 2; yTry = yOld + yMove, k++) {
					if (isSquareOnBoard(content, xTry, yTry)
							&& (content[yTry][xTry] == null || content[yTry][xTry]
									.getFraction() != fraction)) {
						list.add(new Coordinate(xTry, yTry));
					}
				}
			}
		}

		list = removeIrregularyMoves(content, list, xOld, yOld);
		return list;
	}
}
